<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>

	<body>
		<script>
			//冒泡排序
			function BubbleSort(array) {
				var length = array.length;
				for(var i = length - 1; i > 0; i--) {
					for(var j = 0; j < i; j++) {
						if(array[j] > array[j + 1]) {
							var temp = array[j];
							array[j] = array[j + 1];
							array[j + 1] = temp;
						}
					}
				}
				return array;
			}

			var arr = [10, 9, 8, 7, 7, 6, 5, 11, 3];
			var result = BubbleSort(arr);
			console.log(result);

			//插入排序
			function InsertionSort(array) {
				var length = array.length;
				for(var i = 0; i < length - 1; i++) {
					//i代表已经排序好的序列最后一项下标
					var insert = array[i + 1];
					var index = i + 1; //记录要被插入的下标
					for(var j = i; j >= 0; j--) {
						if(insert < array[j]) {
							//要插入的项比它小，往后移动
							array[j + 1] = array[j];
							index = j;
						}
					}
					array[index] = insert;
				}
				return array;
			}

			var arr1 = [100, 90, 80, 62, 80, 8, 1, 2, 39];
			var result1 = InsertionSort(arr);
			console.log(result1);
		</script>
	</body>

</html>